home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow_WinXP / VideoControl / CPP / CompositeControl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  8.1 KB  |  261 lines

  1. //------------------------------------------------------------------------------
  2. // File: CompositeControl.cpp
  3. //
  4. // Desc: Implementation of the CCompositeControl
  5. //       for the Windows XP MSVidCtl C++ sample
  6. //
  7. // Copyright (c) 2001 Microsoft Corporation.  All rights reserved.
  8. //------------------------------------------------------------------------------
  9.  
  10. #include "stdafx.h"
  11. #include "CPPVideoControl.h"
  12. #include "CompositeControl.h"
  13.  
  14. #include <atlbase.h>
  15. #include <msvidctl.h>
  16. #include <tuner.h>
  17. #include <segment.h>
  18.  
  19. #define  DEFAULT_CHANNEL 46
  20.  
  21. #define  STR_VIEW_FAILURE  TEXT("Failed IMSVidCtl::View.  You may not have ") \
  22.              TEXT("properly installed your hardware.  Your ATSC tuner card, ") \
  23.              TEXT("MPEG-2 decoder, or video card may be incompatible with ")   \
  24.              TEXT("the MicrosoftTV Technologies architecture.")
  25.  
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CCompositeControl
  29.  
  30. LRESULT CCompositeControl::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  31. {
  32.     HRESULT hr = S_OK;
  33.     ITuningSpace * pTuningSpace = NULL;
  34.     IEnumTuningSpaces * pEnumTuningSpaces = NULL;
  35.     ITuningSpace ** pTuningSpaceArray = NULL;
  36.     ULONG ulNumFetched = 0;
  37.     long lCount = 0;
  38.     ULONG i = 0;
  39.     CComBSTR bstrATSC = L"ATSC";
  40.     CComBSTR bstrTemp = L"";
  41.  
  42.     // Get window handle of channel label (for later update)
  43.     m_hwndChannelID = GetDlgItem(IDC_CHANNELID);
  44.  
  45.     // Get the tuning space collection
  46.     hr = CoCreateInstance(CLSID_SystemTuningSpaces, NULL, 
  47.             CLSCTX_INPROC_SERVER, IID_ITuningSpaceContainer,
  48.             reinterpret_cast<void**>(&m_pTuningSpaceContainer));
  49.     if (FAILED(hr))
  50.     {
  51.         MessageBox(TEXT("Failed to create system tuning space."), TEXT("Error"), MB_OK);
  52.         return hr;
  53.     }
  54.   
  55.     // Get the video control object
  56.     hr = GetDlgControl(IDC_VIDEOCONTROL1,IID_IMSVidCtl, reinterpret_cast<void **>(&m_pMSVidCtl));
  57.     if(m_pMSVidCtl == NULL) 
  58.     {
  59.         MessageBox(TEXT("Failed to get Video Control on main dialog!"),TEXT("Error"),MB_OK);
  60.         return hr;
  61.     }
  62.  
  63.     // Find the ATSC tuning space in the collection
  64.     hr = m_pTuningSpaceContainer->get_EnumTuningSpaces(&pEnumTuningSpaces);
  65.     if (FAILED(hr))
  66.     {
  67.         MessageBox(TEXT("Failed to get tuning space enumerator."), TEXT("Error"), MB_OK);
  68.         return hr;
  69.     }
  70.     hr = m_pTuningSpaceContainer->get_Count(&lCount);
  71.     if (FAILED(hr))
  72.     {
  73.         MessageBox(TEXT("Failed to enumerate tuning spaces."), TEXT("Error"), MB_OK);
  74.         return hr;
  75.     }
  76.  
  77.     // Read tuning space info into allocated array
  78.     pTuningSpaceArray = new ITuningSpace*[lCount];
  79.     hr = pEnumTuningSpaces->Next(lCount, pTuningSpaceArray, &ulNumFetched);
  80.     if (FAILED(hr))
  81.     {
  82.         MessageBox(TEXT("Failed to read tuning spaces."), TEXT("Error"), MB_OK);
  83.         return hr;
  84.     }
  85.  
  86.     // Find the ATSC tuning space
  87.     for (i = 0;i < ulNumFetched; i++)
  88.     {
  89.         hr = pTuningSpaceArray[i]->get_UniqueName(&bstrTemp);
  90.         if (FAILED(hr))
  91.         {
  92.             MessageBox(TEXT("Failed to read tuning space unique name."), TEXT("Error"), MB_OK);
  93.             return hr;
  94.         }
  95.  
  96.         // Is this the ATSC tuning space?
  97.         if (bstrTemp == bstrATSC)
  98.         {
  99.             hr = pTuningSpaceArray[i]->Clone(&pTuningSpace);
  100.             break;
  101.         }
  102.     }
  103.  
  104.     if (pTuningSpace == NULL)
  105.     {
  106.         MessageBox(TEXT("Could not find ATSC tuning space."), TEXT("Error"), MB_OK);
  107.         return hr;
  108.     }
  109.     
  110.     // QI for IATSCTuningSpace
  111.     hr = pTuningSpace->QueryInterface(IID_IATSCTuningSpace, reinterpret_cast<void**>(&m_pATSCTuningSpace));
  112.     if (FAILED(hr))
  113.     {
  114.         MessageBox(TEXT("Failed to QI for IATSCTuningSpace."), TEXT("Error"), MB_OK);
  115.         return hr;
  116.     }
  117.  
  118.     // Create ATSC Locator
  119.     hr = CoCreateInstance(CLSID_ATSCLocator, NULL, 
  120.             CLSCTX_INPROC_SERVER, IID_IATSCLocator,
  121.             reinterpret_cast<void**>(&m_pATSCLocator));
  122.     if (FAILED(hr))
  123.     {
  124.         MessageBox(TEXT("Failed to create ATSC locator."), TEXT("Error"), MB_OK);
  125.         return hr;
  126.     }
  127.  
  128.     hr = SetChannel(DEFAULT_CHANNEL);
  129.     if (FAILED(hr))
  130.     {
  131.         // SetChannel will give a message box indicating the error
  132.         return hr;
  133.     }
  134.  
  135.     // Start viewing digital TV
  136.     hr = m_pMSVidCtl->Run();
  137.     if (FAILED(hr))
  138.     {
  139.         MessageBox(TEXT("Failed IMSVidCtl::Run.  You may not have properly installed your hardware.  Your ATSC tuner card, MPEG-2 decoder, or video card may be incompatible with the MicrosoftTV Technologies architecture."), TEXT("Error"), MB_OK);
  140.         return hr;
  141.     }
  142.  
  143.     SAFE_RELEASE(pTuningSpace);
  144.     SAFE_RELEASE(pEnumTuningSpaces);
  145.     delete pTuningSpaceArray;
  146.  
  147.     return hr;
  148. };
  149.  
  150.  
  151. LRESULT CCompositeControl::OnClickedChanneldown(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  152. {
  153.     HRESULT hr = S_OK;
  154.     long lPhysicalChannel = 0;
  155.     
  156.     // Get the current physical channel and decrement it
  157.     hr =m_pATSCLocator->get_PhysicalChannel(&lPhysicalChannel);
  158.     if (FAILED(hr))
  159.     {
  160.         MessageBox(TEXT("Failed to read physical channel."), TEXT("Error"), MB_OK);
  161.         return hr;
  162.     }
  163.     lPhysicalChannel--;
  164.  
  165.     hr = SetChannel(lPhysicalChannel);
  166.     return hr;
  167. };
  168.  
  169. LRESULT CCompositeControl::OnClickedChannelup(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  170. {
  171.     HRESULT hr = S_OK;
  172.     long lPhysicalChannel = 0;
  173.     
  174.     // Get the current physical channel and increment it
  175.     hr =m_pATSCLocator->get_PhysicalChannel(&lPhysicalChannel);
  176.     if (FAILED(hr))
  177.     {
  178.         MessageBox(TEXT("Failed to read physical channel."), TEXT("Error"), MB_OK);
  179.         return hr;
  180.     }
  181.     lPhysicalChannel++;
  182.  
  183.     hr = SetChannel(lPhysicalChannel);
  184.     return hr;
  185. };
  186.  
  187. HRESULT CCompositeControl::SetChannel(long lPhysicalChannel)
  188. {
  189.     HRESULT hr=S_OK;
  190.     IATSCChannelTuneRequest * pATSCTuneRequest = NULL;
  191.     ITuneRequest * pTuneRequest = NULL;
  192.  
  193.     // Set the Physical Channel
  194.     hr =m_pATSCLocator->put_PhysicalChannel(lPhysicalChannel);
  195.     if (FAILED(hr))
  196.     {
  197.         MessageBox(TEXT("Failed to set physical channel."), TEXT("Error"), MB_OK);
  198.         return hr;
  199.     }
  200.  
  201.     // Create an ATSC tune request
  202.     hr = m_pATSCTuningSpace->CreateTuneRequest(&pTuneRequest);
  203.     if (FAILED(hr))
  204.     {
  205.         MessageBox(TEXT("Failed to create tune request."), TEXT("Error"), MB_OK);
  206.         return hr;
  207.     }
  208.  
  209.     hr = pTuneRequest->QueryInterface(IID_IATSCChannelTuneRequest, reinterpret_cast<void**>(&pATSCTuneRequest));
  210.     if (FAILED(hr))
  211.     {
  212.         MessageBox(TEXT("Failed to query for IATSCChannelTuneRequest."), TEXT("Error"), MB_OK);
  213.         return hr;
  214.     }
  215.  
  216.     // Set the Channel and MinorChannel property on the tune request to -1
  217.     // These properties will get set by the network provider once tuned to a ATSC channel
  218.     hr = pATSCTuneRequest->put_Channel(-1);
  219.     if (FAILED(hr))
  220.     {
  221.         MessageBox(TEXT("Failed to put channel property."), TEXT("Error"), MB_OK);
  222.         return hr;
  223.     }
  224.     hr = pATSCTuneRequest->put_MinorChannel(-1);
  225.     if (FAILED(hr))
  226.     {
  227.         MessageBox(TEXT("Failed to put minor channel property."), TEXT("Error"), MB_OK);
  228.         return hr;
  229.     }
  230.     hr = pATSCTuneRequest->put_Locator(m_pATSCLocator);
  231.     if (FAILED(hr))
  232.     {
  233.         MessageBox(TEXT("Failed to put locator."), TEXT("Error"), MB_OK);
  234.         return hr;
  235.     }
  236.  
  237.     // Now that the tune request is configured, pass it to the video control
  238.     CComVariant var = pATSCTuneRequest;
  239.     hr = m_pMSVidCtl->View(&var);
  240.     if (FAILED(hr))
  241.     {
  242.         MessageBox(STR_VIEW_FAILURE, TEXT("Error"), MB_OK);
  243.         return hr;
  244.     }
  245.  
  246.     ShowChannelNumber(lPhysicalChannel);
  247.  
  248.     // Release interfaces
  249.     pATSCTuneRequest->Release();
  250.     pTuneRequest->Release();
  251.     return hr;
  252. }
  253.  
  254. void CCompositeControl::ShowChannelNumber(long lChannel)
  255. {
  256.     TCHAR szChannelNumber[8];
  257.     wsprintf(szChannelNumber, TEXT("%d\0"), lChannel);
  258.     SetDlgItemText(IDC_CHANNELID, szChannelNumber);
  259. }
  260.  
  261.